﻿




using System;
using System.Collections.Generic;
using System.Linq;
using Hotpot.Infrastructure;
using Hotpot.Models;
using Hotpot.Modules.Blogs.Models;

namespace Hotpot.Modules.Blogs.Repositories.SqlServer
{
    public class SqlServerTagRepository : IBlogsTagRepository
    {
        private readonly HotpotBlogsDataContext context;
        private readonly Guid siteID;

        public SqlServerTagRepository(HotpotBlogsDataContext context, HotpotContext hotpotContext)
        {
            this.context = context;
            siteID = hotpotContext.Site.ID;
        }

        #region IBlogsTagRepository Members

        public IEnumerable<KeyValuePair<PostTag, int>> GetTagsWithPostCount()
        {
            return (
                       from tt in
                           (from t in context.hotpot_Tags
                            join ptr in context.hotpot_Blogs_PostTagRelationships on t.TagID equals ptr.TagID
                            join p in context.hotpot_Blogs_Posts on ptr.PostID equals p.PostID
                            where
                                p.State == (byte) EntityState.Normal &&
                                p.PublishedDate <= DateTime.Now.ToUniversalTime()
                            select new {Tag = t.hotpot_Tag1, DisplayName = ptr.TagDisplayName, Post = p})
                       group tt by new {Tag = tt.Tag, DisplayName = tt.DisplayName}
                       into results
                           where results.Key.Tag.TagID == results.Key.Tag.ParentTagID
                           orderby results.Key.Tag.TagName
                           select
                           new KeyValuePair<PostTag, int>(
                           new PostTag(results.Key.Tag.TagID, results.Key.Tag.TagName, results.Key.DisplayName,
                                       results.Key.Tag.CreatedDate), results.Count())
                   ).ToArray();
        }

        public IEnumerable<KeyValuePair<PostTag, int>> GetTagsUsedInBlog(Blog blog)
        {
            return (
                       from tt in
                           (from t in context.hotpot_Tags
                            join ptr in context.hotpot_Blogs_PostTagRelationships on t.TagID equals ptr.TagID
                            join p in context.hotpot_Blogs_Posts on ptr.PostID equals p.PostID
                            where
                                p.BlogID == blog.ID && p.State == (byte) EntityState.Normal &&
                                p.PublishedDate <= DateTime.Now.ToUniversalTime()
                            select new {Tag = t.hotpot_Tag1, Post = p})
                       group tt by tt.Tag
                       into results
                           where results.Key.TagID == results.Key.ParentTagID
                           orderby results.Key.TagName
                           select
                           new KeyValuePair<PostTag, int>(
                           new PostTag(results.Key.TagID, results.Key.TagName, results.Key.TagName,
                                       results.Key.CreatedDate),
                           results.Count())
                   ).ToArray();
        }

        #endregion
    }
}
